<?php
namespace Admin\Controller;
use Think\Controller;
class TeacherController extends BaseControlle {
    public function __construct()
    {
        parent::__construct();

    }

    public function index(){
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        $this->display('index');
    }

    public function NewTeacherUI(){
        $this->display();
    }


    public function NewTeacher(){
        $name   = I("post.name");
        $mobile = I("post.mobile");
        $group  = I("post.group");
        $group  = intval($group);
        $user = M('user');
        $is_mobile = $user->where("mobile='$mobile'")->find();
        if($is_mobile){
            $this->ajaxReturn(array('code'=>501,'msg'=>'该手机号已存在!'));
        }
        $prefix = GeneratePassword(10);
        $db = array(
            'name'=>$name,
            'prefix'=>$prefix,
            'password'=>AdminPasswordEncrypt($mobile,$prefix),
            'mobile'=>$mobile,
            'group'=>$group
            );
        $add_ret = $user->data($db)->add();
        if (!$add_ret){
            $this->ajaxReturn(array('code'=>501,'msg'=>'添加失败!'));
        }else{
            $this->ajaxReturn(array('code'=>200));
        }
    }

    public function EditTeacher(){
        $uid = I("get.uid");
        $uid = intval($uid);
        $user = M('user');
        $info = $user->where("uid='$uid'")->find();
        $this->assign('info',$info);
        $this->display();
    }
    public function UpdataTeacher(){
        if(IS_POST){
            $uid   = I("post.uid");
            $name   = I("post.name");
            $mobile = I("post.mobile");
            $group  = I("post.group",2,'intval');
            $user = M('user');
            $is_mobile = $user->where("mobile='$mobile' AND uid!='$uid'")->find();
            $is_uid = $user->where(array('uid'=>$uid))->find();
            if($is_mobile){
                $this->ajaxReturn(array('code'=>501,'msg'=>'该手机号已存在!'));
            }
            if(!$is_uid){
                $this->ajaxReturn(array('code'=>501,'msg'=>'用户不存在!'));
            }
            $prefix = GeneratePassword(10);
            $db = array(
                'name'=>$name,
                'prefix'=>$prefix,
                'password'=>AdminPasswordEncrypt($mobile,$prefix),
                'mobile'=>$mobile,
                'group'=>$group
            );
            $updata_ret = $user->where(array('uid'=>$uid))->save($db);
            if (!$updata_ret){
                $this->ajaxReturn(array('code'=>501,'msg'=>'修改失败!'));
            }else{
                $this->ajaxReturn(array('code'=>200));
            }
        }else{
            $this->ajaxReturn(array('code'=>501,'msg'=>'请按照要求使用程序!'));
        }
    }

    public function DeleteTeacher(){
        if(IS_POST){
            $uid = I('post.uid');
            $uid = intval($uid);

            $user = M('user');
            $ret = $user->where("uid='$uid'")->delete();
            if($ret){
                $this->ajaxReturn(array('code'=>200));
            }else{
                $this->ajaxReturn(array('code'=>5011,'msg'=>'删除失败！'));
            }
        }else{
            $this->ajaxReturn(array('code'=>501,'msg'=>'请按照要求使用程序!'));
        }
    }




    public function Manage(){
        $page = I('get.page');
        $user = M('user');
        $count = $user->count();
        $page = DBPage($count,30,$page);

        $list_user = $user->page($page['page'])->order('uid asc')->select();

        $this->assign('count',$page['countPage']);
        $this->assign('page',$page['page']);
        $this->assign('list_user',$list_user);
        $this->display();
    }

}